package com.muyi.dao.impl;

import com.muyi.pojo.entity.Account;
import com.muyi.pojo.entity.Comment;
import com.muyi.pojo.dto.CommentDTO;
import com.muyi.util.BaseDaoImpl;

import java.util.ArrayList;
import java.util.List;

public class CommentDaoImpl implements com.muyi.dao.CommentDao {
    private BaseDaoImpl baseDao = new BaseDaoImpl();
    

    /**
     * 新增评论信息
     * @param comment
     * @return
     * @throws Exception
     */
    @Override
    public int addComment(Comment comment) throws Exception {
        String sql = "insert into comment values(null,?,?,?,?,?,?)";
        int i = baseDao.cudMethod(sql, comment.getPaper_id(), comment.getUser_id(), comment.getPublish_date(), comment.getLike_count(),
                comment.getFavorite_count(), comment.getContent());
        return i;
    }

    @Override
    public int deleteCommentById(Integer comment_id) throws Exception {
        String sql = "delete from comment where comment_id = ?";
        return baseDao.cudMethod(sql, comment_id);
    }

    @Override
    public int updateComment(Comment comment) throws Exception {
        String sql = "update comment set paper_id= ?, user_id= ?, publish_date=?, like_count = ?," +
                "favorite_count=?, content=? where comment_id = ?";
        int i = baseDao.cudMethod(sql, comment.getPaper_id(), comment.getUser_id(), comment.getPublish_date(), comment.getLike_count(),
                comment.getFavorite_count(), comment.getContent(), comment.getComment_id());
        return i;
    }

    @Override
    public List<CommentDTO> getCommentBySearch(CommentDTO commentdto, Integer pageNo, Integer pageSize) throws Exception {
        //定义一个参数的集合
        ArrayList<Object> paramList = new ArrayList<>();
        String sql1 = "SELECT c.comment_id, c.publish_date, c.like_count, c.favorite_count, p.paper_title, c.content, p.view_count, u.nickname " +
                " from comment as c left join paper as p on c.paper_id = p.paper_id left join user as u on c.user_id = u.user_id " +
                " WHERE 1=1 ";
        if (-1 != commentdto.getPaper_id() && !"".equals(commentdto.getPaper_id())){
            sql1 += " and c.paper_id = " + commentdto.getPaper_id();
        }
        if (-1 != commentdto.getUser_id() && !"".equals(commentdto.getUser_id())){
            sql1 += " and c.user_id = " + commentdto.getUser_id();
        }
        if (null != commentdto.getContent() && !"".equals(commentdto.getContent())){
            sql1 += " and c.content like '%" + commentdto.getContent()+"%'";
        }

        sql1 += " limit ?,?";
        paramList.add((pageNo-1)*pageSize);
        paramList.add(pageSize);

        //执行sql语句
        List<CommentDTO> commentdtoList = baseDao.queryMethod(sql1, CommentDTO.class, paramList.toArray());
        return commentdtoList;
    }

    @Override
    public long getTotal(CommentDTO commentdto) throws Exception {
        //定义一个参数的集合
        ArrayList<Object> paramList = new ArrayList<>();
        String sql1 = "SELECT c.comment_id, c.publish_date, c.like_count, c.favorite_count, p.paper_title, c.content, p.view_count, u.nickname " +
                " from comment as c join paper as p on c.paper_id = p.paper_id left join user as u on c.user_id = u.user_id " +
                " WHERE 1=1 ";
        if (-1 != commentdto.getPaper_id() && !"".equals(commentdto.getPaper_id())){
            sql1 += " and c.paper_id = " + commentdto.getPaper_id();
        }
        if (-1 != commentdto.getUser_id() && !"".equals(commentdto.getUser_id())){
            sql1 += " and c.user_id = " + commentdto.getUser_id();
        }
        if (null != commentdto.getContent() && !"".equals(commentdto.getContent())){
            sql1 += " and c.content like '%" + commentdto.getContent()+"%'";
        }

        //执行sql语句
        List<CommentDTO> commentList = baseDao.queryMethod(sql1, CommentDTO.class, paramList.toArray());
        return commentList.size();
    }

    @Override
    public Comment getCommentById(Integer comment_id) throws Exception {
        String sql = "SELECT *" +
                " FROM comment " +
                " WHERE comment_id = ? ";
        List<Comment> commentList = baseDao.queryMethod(sql, Comment.class, comment_id);
        if (commentList != null && commentList.size() > 0) {
            return commentList.get(0);
        }
        return null;
    }

    @Override
    public int deleteCommentByIds(Integer[] comment_ids) throws Exception {
        // 动态生成 IN 子句
        StringBuilder placeholders = new StringBuilder();
        for (int i = 0; i < comment_ids.length; i++) {
            placeholders.append("?");
            if (i < comment_ids.length - 1) {
                placeholders.append(", ");
            }
        }
        // 生成最终的 SQL 语句
        String sql = "delete from comment where comment_id in (" + placeholders.toString() + ")";

        // 使用 ids 数组作为参数调用 cudMethod 执行 SQL 删除操作
        return baseDao.cudMethod(sql, comment_ids);

    }


}
